package cn.reinforce.util.aliyun;

import cn.reinforce.util.aliyun.model.imm.ImmFile;
import cn.reinforce.util.aliyun.model.imm.ImmPermission;
import cn.reinforce.util.aliyun.model.imm.ImmUser;
import cn.reinforce.util.aliyun.model.imm.ImmWatermark;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.imm.model.v20170906.CreateOfficeConversionTaskRequest;
import com.aliyuncs.imm.model.v20170906.CreateOfficeConversionTaskResponse;
import com.aliyuncs.imm.model.v20170906.GetOfficeConversionTaskRequest;
import com.aliyuncs.imm.model.v20170906.GetOfficeConversionTaskResponse;
import com.aliyuncs.imm.model.v20170906.GetWebofficeURLRequest;
import com.aliyuncs.imm.model.v20170906.GetWebofficeURLResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 阿里云智能媒体管理工具
 *
 * @author 幻幻Fate
 * @create 2019/12/4
 * @since
 */
public class AliyunImmUtil {

    public static String getPreviewDocUrl(String bucket, String immBucket, String projectName, String dir, String filename, String folder, String tgtType, Integer displayDpi) throws ClientException {
        Aliyun aliyun = Aliyun.INSTANCE;

        if (StringUtils.isEmpty(tgtType)) {
            tgtType = "vector";
        }

        if (displayDpi == null || displayDpi == 0) {
            displayDpi = 256;
        }

        // 创建文档转换异步请求任务
        CreateOfficeConversionTaskRequest req = new CreateOfficeConversionTaskRequest();
        req.setProject(projectName);
        // 设置待转换对文件OSS路径
        req.setSrcUri("oss://" + bucket + "/" + dir + filename);
        // 设置文件输出格式为 vector
        req.setTgtType(tgtType);
        // 设置转换后的输出路径
        req.setTgtUri("oss://" + immBucket + "/output/" + filename.substring(0, filename.lastIndexOf(".")) + (StringUtils.isEmpty(folder) ? "" : ("/" + folder)));
        req.setDisplayDpi(displayDpi);
        CreateOfficeConversionTaskResponse res = aliyun.getImmClient().getAcsResponse(req);
        String taskId = res.getTaskId();

        // 获取文档转换任务结果，最多轮询 30 次
        // 每次轮询的间隔为 1 秒
        GetOfficeConversionTaskRequest getOfficeConversionTaskRequest = new GetOfficeConversionTaskRequest();
        getOfficeConversionTaskRequest.setProject(projectName);
        getOfficeConversionTaskRequest.setTaskId(taskId);
        int maxCount = 30;
        int count = 0;
        try {
            while (true) {
                Thread.sleep(1000); // 1 秒
                GetOfficeConversionTaskResponse getOfficeConversionTaskResponse = aliyun.getImmClient().getAcsResponse(getOfficeConversionTaskRequest);
                if (!getOfficeConversionTaskResponse.getStatus().equals("Running")) {
                    // 输出文档转换任务执行结果
                    System.out.println(getOfficeConversionTaskResponse.getTaskId());
                    System.out.println(getOfficeConversionTaskResponse.getFailDetail().getCode());
                    System.out.println("Done");
                    break;
                }
                count = count + 1;
                if (count >= maxCount) {
                    System.out.println("OfficeConversion Timeout for 30 seconds");
                    break;
                }
                System.out.println("Task is still running.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return aliyun.getImmOssEndpoint() + "/output/" + filename.substring(0, filename.lastIndexOf(".")) + (StringUtils.isEmpty(folder) ? "" : ("/" + folder));
    }

    public static GetWebofficeURLResponse getWebOfficeURL(ImmFile file, String fileId, String projectName, ImmUser user, ImmWatermark watermark, ImmPermission permission) throws ClientException {
        Aliyun aliyun = Aliyun.INSTANCE;

        Gson gson = new GsonBuilder().create();

        if (permission == null) {
            permission = new ImmPermission();
        }

        // 创建文档转换异步请求任务
        GetWebofficeURLRequest req = new GetWebofficeURLRequest();
        List<ImmFile> files = new ArrayList<>();
        files.add(file);
        System.out.println(gson.toJson(files));
        req.setFile(gson.toJson(files));
        req.setFileID(fileId);
        req.setProject(projectName);
        req.setPermission(gson.toJson(permission));
        req.setUser(gson.toJson(user));
        req.setWatermark(gson.toJson(watermark));
        GetWebofficeURLResponse getWebofficeURLResponse = aliyun.getImmClient().getAcsResponse(req);
        System.out.println(getWebofficeURLResponse.getRequestId());
        return getWebofficeURLResponse;
    }
}
